package com.h3c.gm.controller

import com.h3c.gm.common.Response
import com.h3c.gm.common.utils.UUIDUtils
import com.h3c.gm.entity.DO.BusHostSpecDO
import com.h3c.gm.entity.DO.BusStorageSpecDO
import com.h3c.gm.entity.DO.BusUserDO
import com.h3c.gm.entity.DO.SysOrgQuotaDO
import com.h3c.gm.service.BusHostSpecService
import com.h3c.gm.service.BusStorageSpecService
import com.h3c.gm.service.BusUserService
import com.h3c.gm.service.SysOrgQuotaService
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController

import javax.annotation.Resource

/**
 * Mock数据工具类
 * Created by d14287 on 2017/6/28.
 */
@RestController
class GeDataController {
    @Resource
    BusUserService busUserService
    @Resource
    BusHostSpecService busHostSpecService
    @Resource
    BusStorageSpecService busStorageSpecService
    @Resource
    SysOrgQuotaService sysOrgQuotaService

    @RequestMapping(value = 'ge/user/{xue}', method = RequestMethod.GET)
    def geUser(@PathVariable(name = 'xue') String xue) {
        def num = 5 + new Random().nextInt(5)
        //def num = 40 + new Random().nextInt(20)
        def i = 0
        while (i < num) {
            def busUserDO = new BusUserDO()
            busUserDO.setUserid(UUIDUtils.generateUUID())
            busUserDO.setOrgkey(xue)
            busUserDO.setName('无名')
            def now = DateTime.now()
            busUserDO.setRegtime(now.minusMonths(new Random().nextInt(9)).toDate())
            busUserService.insert(busUserDO)
            i++
        }
        def response = new Response()
        response.ofSuccess('finished')
    }

    @RequestMapping(value = 'ge/host/{xue}', method = RequestMethod.GET)
    def geHost(@PathVariable(name = 'xue') String xue) {
        def num = 10 + new Random().nextInt(5)

        // /def num = 40 + new Random().nextInt(20)
        def i = 0
        while (i < num) {
            def busHostSpecDO = new BusHostSpecDO()
            busHostSpecDO.setId(UUIDUtils.generateUUID())
            busHostSpecDO.setUserid(UUIDUtils.generateUUID())
            busHostSpecDO.setOrgkey(xue)
            if ((new Random().nextInt(100) % 2) == 0) {
                short ram = 2
                busHostSpecDO.setCpus(ram)
                busHostSpecDO.setRam(2048L)
            } else {
                if ((new Random().nextInt(100) % 2) == 0) {
                    short ram = 4
                    busHostSpecDO.setCpus(ram)
                    busHostSpecDO.setRam(4096L)
                } else {
                    short ram = 8
                    busHostSpecDO.setCpus(ram)
                    busHostSpecDO.setRam(8192L)
                }
            }
            def now = DateTime.now()
            busHostSpecDO.setCreatetime(now.minusMonths(new Random().nextInt(9)).toDate())
            busHostSpecService.insert(busHostSpecDO)
            i++
        }
        def response = new Response()
        response.ofSuccess('finished')
    }

    @RequestMapping(value = 'ge/storage/{xue}', method = RequestMethod.GET)
    def geStorage(@PathVariable(name = 'xue') String xue) {
        def num = 5 + new Random().nextInt(5)
        //def num = 40 + new Random().nextInt(20)
        def i = 0
        while (i < num) {
            def busStorageSpecDO = new BusStorageSpecDO()
            busStorageSpecDO.setId(UUIDUtils.generateUUID())
            busStorageSpecDO.setSize(500)
            busStorageSpecDO.setUserid(UUIDUtils.generateUUID())
            busStorageSpecDO.setOrgkey(xue)
            busStorageSpecDO.setAlreaduse(50 + (new Random().nextInt(50)))
            def now = DateTime.now()
            busStorageSpecDO.setCreatetime(now.minusMonths(new Random().nextInt(9)).toDate())
            busStorageSpecService.insert(busStorageSpecDO)
            i++
        }
        def response = new Response()
        response.ofSuccess('finished')
    }

    @RequestMapping(value = 'ge/quota/{xue}', method = RequestMethod.GET)
    def geQuota(@PathVariable(name = 'xue') String xue) {

        //cpu  qtype=1
        def sysOrgQuotaDO = new SysOrgQuotaDO()
        sysOrgQuotaDO.setId(UUIDUtils.generateUUID())
        sysOrgQuotaDO.setOrgkey(xue)
        sysOrgQuotaDO.setQuotacount(25)
        sysOrgQuotaDO.setQuotaused(3)
        sysOrgQuotaService.insert(sysOrgQuotaDO)

        //ram qtype=
    }


}
